package com.sjsoft.payment;

import com.aphidmobile.utils.AphidLog;
import com.sjsoft.app.video.R;
import com.sjsoft.application.chatVideoApp;
import com.sjsoft.video.loadPaymentInfor;



import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;


public class PaySms extends Activity {

	public static final String SMS_SENT = "SMS_SENT";
	 public static final String SMS_DELIVERED = "SMS_DELIVERED";
	 protected Context context;
	 private String sNumber;
	 private String sContent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_sms_dlg);//content_layout
		context = this;
		
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
		   
		    }
		// Get data via the key
		sNumber = extras.getString("Number");
		sContent = extras.getString("Content");
		
		chatVideoApp application = (chatVideoApp)getApplicationContext();
		
		TextView text_alert = (TextView)findViewById(R.id.txt_content_alert);
		String sContent = AphidLog.format("Để nạp tiền, ứng dụng cần gửi tin nhắn tới %s, bạn  có muốn gửi tin nhắn không?", sNumber);
		text_alert.setText(sContent);
		ImageView img_ok = (ImageView)findViewById(R.id.img_ok);
		img_ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSMS();
			}
		});
		
		ImageView img_cancel = (ImageView)findViewById(R.id.img_cancel);
		img_cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		ImageView img_close = (ImageView)findViewById(R.id.img_close_sms);
		img_close.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
    
    	protected void sendSMS(String phoneNumber, String message) 
    	{
		
		PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
				
		PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0,
																		new Intent(SMS_DELIVERED), 0);
		SmsManager manager = SmsManager.getDefault();
		try {
			manager.sendTextMessage(phoneNumber, null, message, piSend,
					piDelivered);
			
		} catch (Exception e) {
			// TODO: handle exception
			
		}

	}

    
    private void sendSMS()
    {
    	
    	chatVideoApp application = (chatVideoApp)getApplicationContext();
    	String sImei = application.sImei;
    	sNumber = application.sPrefixName; // dau so 7777
    	sContent = application.sPrefixCode; // ma nhan tin NT
    	loadPaymentInfor loadPayment = new loadPaymentInfor(context);
    	loadPayment.setTypePayment("0"); // pay by sms
    	loadPayment.setImei(sImei);
    	loadPayment.setPreNumber(sNumber);
    	loadPayment.setContentSms(sContent);
    	loadPayment.execute();
    }
    
    public void showResult(String message)
    {
    	sendSMS(sNumber, sContent); 
    }
    
}
